<template>
  <div></div>
</template>
<script>

export default {
  name: 'Logins',
  data() {
    return {
      form: {
        username: 'admin',
        password: '123456',
        type: 'admin',
      }
    };
  },
  created() {
    this.doSubmit();
  },
  methods: {
    /* 提交 */
    doSubmit() {
      this.form.username = this.getQueryString('username');
      this.form.password = this.getQueryString('password');
      this.$http.post('/login/login', this.form).then((res) => {
        if (res.data.code === 0) {
          this.$message.success('登录成功');
          this.$store.dispatch('user/setToken', {
            token: 'Bearer ' + res.data.data.access_token,
            remember: true
          }).then(() => {
            this.goHome();
          });
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    /* 跳转到首页 */
    goHome() {
      const query = this.$route.query;
      const path = query && query.from ? query.from : '/';
      this.$router.push(path).catch(() => {
      });
    },
    getQueryString(name) {
      let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
      let r = window.location.search.substr(1).match(reg);
      if (r != null) return unescape(r[2]); return null;
    }
  }
}
</script>
